package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.heima.model.common.enums.AppHttpCodeEnum.*;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    /**
     *      查找频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //1.1  数据检查
        if (dto == null) {
            return ResponseResult.errorResult(DATA_NOT_EXIST,"数据不存在");
        }
        //1.2  分页参数检查
        dto.checkParam();

        //2.1  按名称模糊分页查找
        Page page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = Wrappers.lambdaQuery();

        if (StringUtils.isNotBlank(dto.getName())) {
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        //2.2  添加排序
        lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
        IPage result = page(page, lambdaQueryWrapper);
        //3.1  结果封装
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());
        responseResult.setData(result.getRecords());



        return responseResult;
    }

    /**
     *      添加频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult addChannel(AdChannel adChannel) {
        //1.1 参数校验
        if (adChannel == null||!StringUtils.isNotBlank(adChannel.getName())) {
            return ResponseResult.errorResult(PARAM_REQUIRE);
        }
        //2.1 判断频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,adChannel.getName()));
        if (count > 0) {
            return ResponseResult.errorResult(DATA_EXIST);
        }

        //3.1  执行新增
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //4.1  返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //1.1  参数校验
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(PARAM_REQUIRE);
        }
        //2.1  检测新频道名是否存在  根据id


        //传进来的新名字
        String newChannelName = adChannel.getName();
        //如果我的频道名字没有变，我就直接 save
        //通过id 查出 我的旧频道信息
        AdChannel oldChannel = this.getById(adChannel.getId());
        if (oldChannel == null) {
            return ResponseResult.errorResult(DATA_NOT_EXIST,"频道不存在");
        }
        //如果我的名字改了，我要差新名字有没有存在
        if (!oldChannel.getName().equals(newChannelName)) {
            LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = Wrappers.<AdChannel>lambdaQuery();
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newChannelName));
            if (count > 0) {
                return ResponseResult.errorResult(DATA_NOT_EXIST,"新频道已存在");
            }
            this.updateById(adChannel);

            return ResponseResult.okResult();

        }
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult deleteChannel(Integer id) {

        if (id == null) {
            return ResponseResult.errorResult(PARAM_REQUIRE);
        }
        AdChannel adChannel = this.getById(id);
        if (adChannel == null) {
            return ResponseResult.errorResult(DATA_NOT_EXIST,"频道不存在");
        }
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(DATA_NOT_ALLOW,"此频道未下架");
        }

        this.removeById(id);
        if (id == 6) {
            throw  new CustomException(DATA_NOT_ALLOW,"此 ID 无法删除");

        }
        return ResponseResult.okResult();
    }


}
